package com.ml.carManager.widgets;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.amap.api.maps.MapView;
import com.ml.carManager.R;

import custom.base.log.MLog;
import custom.captcha.utils.DisplayUtil;

public class TouchScrollView extends ScrollView {
    private MapView mapView;
    /**
     * 顶部需要分发时间的高度
     */
    private Integer topHeight = 0;
    /**
     * 当前需要分发事件的高度
     */
    private Integer currentHeight = 0;

    public TouchScrollView(Context context) {
        super(context);
    }

    public TouchScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TouchScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(final Context context, final AttributeSet attrs) {
        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TouchScrollView);
        int top = typedArray.getInteger(R.styleable.TouchScrollView_padding_top, topHeight);
        setTopHeight(top);

//        setPadding(getPaddingLeft(), topHeight, getPaddingRight(), getPaddingBottom());
        getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                currentHeight = topHeight - getScrollY();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float y = event.getY();
        if (y < currentHeight) {
            if (this.mapView != null) {
                this.mapView.onTouchEvent(event);
                return false;
            }
        }

        return super.onTouchEvent(event);
    }

    /**
     * 未设置则不会生效
     */
    public void setView(MapView mapView) {
        this.mapView = mapView;
    }

    /**
     * 传入DP
     */
    public void setTopHeight(Integer topHeight) {
        this.topHeight = DisplayUtil.dip2px(getContext(), topHeight * 1f);
        this.currentHeight = this.topHeight;
    }

    /**
     * 传入DP
     */
    public void setCurrentHeight(Integer currentHeight) {
        this.currentHeight = DisplayUtil.dip2px(getContext(), currentHeight * 1f);
    }

}